<%@ include file="/html/common/init.jsp" %>
<%

String redirect = ParamUtil.getString(request, "redirect");
ShoppingCategory category = (ShoppingCategory)request.getAttribute("SHOPPING_CATEGORY");
long categoryId = BeanParamUtil.getLong(category, request, "categoryId");
long parentCategoryId = BeanParamUtil.getLong(category, request, "parentCategoryId", ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
%>

<%
String parentCategoryName = "";

try {
	ShoppingCategory parentCategory = ShoppingCategoryLocalServiceUtil.getCategory(parentCategoryId);

	parentCategoryName = parentCategory.getName();
}
catch (NoSuchCategoryException nscce) {
}
%>

<portlet:actionURL name="updateProcess" var="editCategoryURL">
   	<portlet:param name="edit1" value="edit1" />
</portlet:actionURL>

<portlet:renderURL var="cancelURL">
		<portlet:param name="jspPage"
			value="/html/category/category_list.jsp" />
	</portlet:renderURL>
	

<portlet:renderURL var="selectCategoryURL" windowState="<%= LiferayWindowState.POP_UP.toString() %>">
	<portlet:param name="jspPage" value="/html/category/select_category.jsp" />
	<portlet:param name="categoryId" value="<%= String.valueOf(parentCategoryId) %>" />
</portlet:renderURL>

<%
	String taglibOpenCategoryWindow = "var categoryWindow = window.open('" + selectCategoryURL + "', 'category', 'directories=no,height=640,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,width=680'); void('');categoryWindow.focus();";
%>	
    
<aui:form action="<%= editCategoryURL %>" method="post" name="fm" onSubmit='<%= renderResponse.getNamespace() + "saveCategory();" %>'>
	<aui:input name="<%= Constants.CMD %>" type="hidden" />
	<aui:input name="redirect" type="hidden" value="<%= redirect %>" />
	<aui:input name="categoryId" type="hidden" value="<%= categoryId %>" />
	<aui:input name="parentCategoryId" type="hidden" value="<%= parentCategoryId %>" />
	
	<liferay-ui:header
		backURL="<%= redirect %>"
		title="category"
	/>
	
	<h6>select parent Id: </h6>
	<aui:a href="#" id="parentCategoryName" label="<%= parentCategoryName %>" />
	<aui:button onClick="<%= taglibOpenCategoryWindow %>" type="button" value="select" />
	<aui:button onClick='<%= renderResponse.getNamespace() + "removeCategory();" %>' type="button" value="remove" />
	
	<aui:fieldset>
		<aui:input cssClass="lfr-input-text-container" name="name" type="text"/>

		<aui:input cssClass="lfr-textarea-container" name="description"  type="textarea"/>
		
		<c:if test="<%= category == null %>">
			<aui:field-wrapper label="permissions">
				<liferay-ui:input-permissions
					modelName="<%= ShoppingCategory.class.getName() %>"
				/>
			</aui:field-wrapper>
		</c:if>
	</aui:fieldset>
	
	<aui:button-row>
		<aui:button type="submit" />
	<a href="<%=cancelURL.toString() %>">	<aui:button  type="button" value="Cancel" /></a>
	</aui:button-row>
</aui:form>

<aui:script>
	function <portlet:namespace />saveCategory() {
		document.<portlet:namespace />fm.<portlet:namespace /><%= Constants.CMD %>.value = "<%= Constants.ADD %>";
		submitForm(document.<portlet:namespace />fm);
	}
	
	Liferay.provide(
		window,
		'<portlet:namespace />removeCategory',
		function() {
			var A = AUI();

			document.<portlet:namespace />fm.<portlet:namespace />parentCategoryId.value = "<%= ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID %>";

			var nameEl = document.getElementById("<portlet:namespace />parentCategoryName");

			nameEl.href = "";
			nameEl.innerHTML = "";

			var mergeWithParent = A.one('#<portlet:namespace />merge-with-parent-checkbox-div');
			var mergeWithParentCategory = A.one('#<portlet:namespace />mergeWithParentCategoryCheckbox');

			if (mergeWithParent) {
				mergeWithParent.hide();
			}

			if (mergeWithParentCategory) {
				mergeWithParentCategory.set('checked', false);
			}
		},
		['aui-base']
	);
	Liferay.provide(
		window,
		'<portlet:namespace />selectCategory',
		function(parentCategoryId, parentCategoryName) {
			var A = AUI();

			document.<portlet:namespace />fm.<portlet:namespace />parentCategoryId.value = parentCategoryId;

			var nameEl = document.getElementById("<portlet:namespace />parentCategoryName");

			nameEl.href = "<portlet:renderURL><portlet:param name="struts_action" value="/message_boards/view" /></portlet:renderURL>&<portlet:namespace />categoryId=" + parentCategoryId;
			nameEl.innerHTML = parentCategoryName + "&nbsp;";

			if (parentCategoryId != <%= ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID %>) {
				var mergeWithParent = A.one('#<portlet:namespace />merge-with-parent-checkbox-div');

				if (mergeWithParent) {
					mergeWithParent.show();
				}
			}
		},
		['aui-base']
	);
</aui:script>				